/*
 页面名称：管理员 -添加员工
 作者: 苗怀雨
 时间:2017年12月31日
 最后编辑：2017年12月31日20:20:13
 */

$(document).ready(function() {
    /*添加图书按钮*/
    $("ul.nav").find("button.btn-success").click(function () {
        toAddBook();
    });

    /* 借出图书列表按钮 */
    $("ul.nav").find("button.btn-warning").click(function () {
        var pageNum = 1;
        var pageSize = $("#pageSize").val();

        var $form = $("<div class='postForm'></div>");
        var $elem = $("<div class='postParam'></div>");

        $elem.attr("id","pageNum");
        $elem.text(pageNum);
        $form.append($elem.clone());

        $elem.attr("id","pageSize");
        $elem.text(pageSize);
        $form.append($elem.clone());

        $elem.attr("id","state");
        $elem.text(1);
        $form.append($elem.clone());

        postForm("bookListPageByState",$form);
    });

    /* 全部图书按钮 */
    $("ul.nav").find("button.btn-primary").click(function () {
        window.location.href ="bookListPage";
    });

    /* 已丢失图书列表按钮 */
    $("ul.nav").find("button.btn-dark").click(function () {
        var pageNum = 1;
        var pageSize = $("#pageSize").val();

        var $form = $("<div class='postForm'></div>");
        var $elem = $("<div class='postParam'></div>");

        $elem.attr("id","pageNum");
        $elem.text(pageNum);
        $form.append($elem.clone());

        $elem.attr("id","pageSize");
        $elem.text(pageSize);
        $form.append($elem.clone());

        $elem.attr("id","state");
        $elem.text(2);
        $form.append($elem.clone());

        postForm("bookListPageByState",$form);
    });

    /* 删除多个图书确认 */
    $(".bs-example-modal-dbg").find(".modal-footer").children("button.btn-primary").click(function () {
        deleteBookGroup();
    })

    /* 搜索图书确认 */
    $(".bs-example-modal-sb").find(".modal-footer").children("button.btn-primary").attr("onclick","searchBook()");

});

/* 删除图书按钮 弹出删除图书提示框*/
function deleteBookInfo(e){
    var $modal = $(".bs-example-modal-db");
    var name = $(e).parents("tr").children('td').eq(2).text();
    var bookId = $(e).parents("tr").children('td').eq(1).text();

    $("h4",$modal).eq(1).text("删除图书:"+ name);       //设置标题名称
    $(".btn-primary",$modal).attr("onclick","deleteBook("+bookId+");");  //设置确定按钮
}

//删除图书
function deleteBook(bookId) {
    var $modal = $(".bs-example-modal-ds");
    var $closeButton = $("button",$modal).eq(1);        //弹出框关闭按钮
    var $ensureButton = $("button",$modal).eq(2);       //弹出框确定按钮

    $ensureButton.attr("disabled","disabled"); //设置编辑确定按钮暂时不可用
    post("deleteBook","bookId",bookId);
}

/* 编辑图书按钮*/
function editBook(e){
    var bookId = $(e).parents("tr").children('td').eq(1).text();
    window.location.href = "editBookPage?bookId=" + bookId;
}

function searchBook() {
    var selects = $(".bs-example-modal-sb").find("select");
    var input = $("#searchKey");
    var input1 = $("<input type='text' name='bookId'>");
    var input2 = $("<input type='text' name='bookName'>");
    var input3 = $("<input type='text' name='author'>");
    var input4 = $("<input type='text' name='ISBN'>");

    input1.css("display","none");
    input1.appendTo("form");
    input2.css("display","none");
    input2.appendTo("form");
    input3.css("display","none");
    input3.appendTo("form");
    input4.css("display","none");
    input4.appendTo("form");

    if(selects.eq(0).val() == 0){
        if(!judgeStringIsNumber(input.val())){
            createPNotify("提示信息","error","ID必须为数字");
            return;
        }
        input1.val(input.val());
    }else if(selects.eq(0).val() == 1){
        input2.val(input.val());
    } else if(selects.eq(0).val() == 2){
        input3.val(input.val());
    }else{
        input4.val(input.val());
    }
    if(input.val() == ""){
        createPNotify("提示信息","error","请输入关键字");
        return;
    }

    $("form").submit();
}

/* 删除多个选定图书 */
function deleteBookGroup() {
    var bookArr = new Array();
    var bookId;
    var $checkboxes = $("input[type='checkbox']");
    var $modal = $(".bs-example-modal-dbg");
    var $prompt = $("h4",$modal).eq(1);
    var $closeButton = $("button",$modal).eq(1);        //弹出框关闭按钮
    var $ensureButton = $("button",$modal).eq(2);       //弹出框确定按钮

    $ensureButton.attr("disabled","disabled");

    var dttable = $("#datatable-checkbox").dataTable();
    var nTrs = dttable.fnGetNodes();

    for(var i = 0;i < nTrs.length;i++){
        if($(nTrs[i]).children("td").eq(0).children("input").prop("checked")){
            bookId = $(nTrs[i]).children("td").eq(1).text();
            bookArr.push(bookId);
        }
    }   //end for

    if(bookArr.length == 0){     //如果没有选定
        // $ensureButton.removeAttr("disabled"); //设置编辑确定按钮可用
        // $closeButton.trigger("click");  //关闭弹出框
        createPNotify("提示","info","请选择要删除的图书");
        return;
    }
    promptInformation($prompt,"success","正在删除...");
    post("deleteBookGroup","bookList",bookArr.toString());
}
function borrowedBookList(e){
    var $this = $(e);
    var pageNum = $this.text();
    var pageSize = $("#pageSize").val();

    var $form = $("<div class='postForm'></div>");
    var $elem = $("<div class='postParam'></div>");

    $elem.attr("id","pageNum");
    $elem.text(pageNum);
    $form.append($elem.clone());

    $elem.attr("id","pageSize");
    $elem.text(pageSize);
    $form.append($elem.clone());

    $elem.attr("id","state");
    $elem.text(1);
    $form.append($elem.clone());

    postForm("bookListPageByState",$form);
}
function lostedBookList(e){
    var $this = $(e);
    var pageNum = $this.text();
    var pageSize = $("#pageSize").val();

    var $form = $("<div class='postForm'></div>");
    var $elem = $("<div class='postParam'></div>");

    $elem.attr("id","pageNum");
    $elem.text(pageNum);
    $form.append($elem.clone());

    $elem.attr("id","pageSize");
    $elem.text(pageSize);
    $form.append($elem.clone());

    $elem.attr("id","state");
    $elem.text(2);
    $form.append($elem.clone());

    postForm("bookListPageByState",$form);
}
function bookList(e) {
    var $this = $(e);
    var pageNum = $this.text();
    var pageSize = $("#pageSize").val();

    var $form = $("<div class='postForm'></div>");
    var $elem = $("<div class='postParam'></div>");

    $elem.attr("id","pageNum");
    $elem.text(pageNum);
    $form.append($elem.clone());

    $elem.attr("id","pageSize");
    $elem.text(pageSize);
    $form.append($elem.clone());

    postForm("bookListPage",$form);
}

function bigImage(src) {
    var $modal = $("div.bigImage");
    var imageSrc = src;

    if(imageSrc == "/library/"){
        imageSrc = "/library/assets/images/no-image.jpg";
    }

    $modal.find("img").attr("src",imageSrc);
}

//找到丢失图书
function findBook(bookId) {
    var $modal = $(".bs-example-modal-fb");
    var $closeButton = $("button",$modal).eq(1);        //弹出框关闭按钮
    var $ensureButton = $("button",$modal).eq(2);       //弹出框确定按钮

    $ensureButton.attr("disabled","disabled"); //设置编辑确定按钮暂时不可用
    post("findBook","bookId",bookId);
}
/* 删除图书按钮 弹出删除图书提示框*/
function findBookInfo(e){
    var $modal = $(".bs-example-modal-fb");
    var bookId = $(e).parents("tr").children('td').eq(1).text();
    var bookName = $(e).parents("tr").children('td').eq(2).text();

    $("h4",$modal).eq(1).text("取消图书丢失状态:"+ bookName);       //设置标题名称
    $(".btn-primary",$modal).attr("onclick","findBook("+bookId+");");  //设置确定按钮
}